﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using ATM.DBlayer;
//using ATM.DBStublayer;



namespace ATM
{
    public class AccountLogic
    {
        public static DataGridView grid = new DataGridView();
        public int check,ch ,mony , information ,senderB, reciverB , resultSender, resultReciver ;
        string AccounN_ID ;
        public int balanceDE, DEbalance , balanceCHAn , checkforBAL;
        public int balanceWD, WDbalance, lessBalan;


        AccountDB d = new AccountDB();



        public void calllog(string ca)
        {
            int x;
            if ((int.TryParse(ca, out x)) && int.Parse(ca) > 0 && !string.IsNullOrEmpty(ca))
            {
                ch = 1;
                d.logoing(ca);
                if (d.count == 1)
                    check = 1;
                else
                    check = 0;
            }
            else
                ch = 0;
        }




        public void callinter(string ca)
        {
            AccountDB.data = grid;
            d.interfaccee(ca);
        }


        public void deposcall(string t, string a)
        {
            int x, y;
            if ((int.TryParse(t, out x)) && int.Parse(t) > 0
                && (int.TryParse(a, out y)) && int.Parse(a) > 0
                && !string.IsNullOrEmpty(t) && !string.IsNullOrEmpty(a))
            {
                ch = 1;
                if (int.Parse(t) <= 10000 && int.Parse(t) >= 10)
                {
                    checkforBAL = 1;
                    
                    d.balance(a);
                    balanceDE = d.chekBalance;
                    DEbalance = int.Parse(t);
                    balanceCHAn = balanceDE + DEbalance;
                    d.balnceChange(a, balanceCHAn);

                    // record the transaction in the DB
                    d.RecordDeposit(a, balanceCHAn, DEbalance);                    
                   // check = 1;
                }
                else
                    checkforBAL = 0;
            }
            else
                ch = 0; 
        }

        public void wdrcall(string te, string ac)
        {

            int x, y;
            if ((int.TryParse(te, out x)) && int.Parse(te) > 0
                && (int.TryParse(ac, out y)) && int.Parse(ac) > 0 
                && !string.IsNullOrEmpty(te) && !string.IsNullOrEmpty(ac))
            {
                ch = 1;
                if (int.Parse(te) <= 5000 && int.Parse(te) >= 10)
                {
                    checkforBAL = 1;

                    d.balance(ac);
                    balanceWD = d.chekBalance;
                    WDbalance = int.Parse(te);
                    if (balanceWD > WDbalance)
                    {
                        lessBalan = 1;
                        balanceCHAn = balanceWD - WDbalance;
                        d.balnceChange(ac, balanceCHAn);
                        
                        // record the transaction in the DB
                        d.RecordWithdraw(ac, balanceCHAn, WDbalance);  
                    }
                    else
                        lessBalan = 0;

                    // check = 1;
                }
                else
                    checkforBAL = 0;
            }
            else
                ch = 0; 

           
        }

        public void tranferB(string sen, string rec, string balance)
        {
            int x, y;

            if ((int.TryParse(rec, out x)) && (int.TryParse(balance, out y)) && int.Parse(rec) > 0 && int.Parse(balance) > 0)
            {
                check = 1;   
                mony = int.Parse(balance);
                d.balance(sen);
                senderB = d.chekBalance;
                d.balanceByID(rec);
                reciverB = d.balanceID;
                d.ID(sen);
                d.AcountNumber(rec);
                AccounN_ID = d.chekAccountNu.ToString();
                if (mony > d.chekBalance || d.chekAccountNu==0  || d.chekId == int.Parse(rec))
                    information = 0;
                else
                {
                    resultSender = senderB - mony;
                    resultReciver = mony + reciverB;
                    d.balnceChange(sen, resultSender);
                    
                    d.balnceChange(AccounN_ID, resultReciver);
                    information = 1;
                    
                    // record the transaction in the DB
                    d.RecordTransfer(sen, resultSender, mony); 
                    
                }

            }
            else
                check = 0;
        }


        public float GetBalance(int accountNumber)
        {

            try
            {
                AccountDB user1 = new AccountDB();
                float Balance = user1.GetBalance(accountNumber);

                return Balance;
            }

            catch
            {

                throw;
            }

        }

        public bool balancechange(string AN, int balance)
        {
            int x, y;
            if ((int.TryParse(AN, out x)) && int.Parse(AN) > 0
                 && balance > 0
                && !string.IsNullOrEmpty(AN) && balance != null)
            {
                d.balnceChange(AN, balance);
                return true;
            }
            else
                return false;
            
        }

        //public

    }
}
